package com.inspur.edp.rule.dsl.core.function.datetime;

import java.time.DayOfWeek;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/12/31 11:57
 */
public class OffsetDateTimeUtils {

    public static long betweenDays(OffsetDateTime start, OffsetDateTime end) {
        OffsetDateTime temp1 = start.withHour(0);
        temp1 = temp1.withMinute(0);
        temp1 = temp1.withSecond(0);
        temp1 = temp1.withNano(0);

        OffsetDateTime temp2 = end.withHour(0);
        temp2 = temp2.withMinute(0);
        temp2 = temp2.withSecond(0);
        temp2 = temp2.withNano(0);

        return ChronoUnit.DAYS.between(temp1, temp2);
    }

    public static OffsetDateTime plusYears(OffsetDateTime time, long years) {
        return time.plusYears(years);
    }


    public static OffsetDateTime plusMonths(OffsetDateTime time, long months) {
        return time.plusMonths(months);
    }

    public static OffsetDateTime plusDays(OffsetDateTime time, long days) {
        return time.plusDays(days);
    }

    public static OffsetDateTime firstDayOfWeek(OffsetDateTime time) {
        return time.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    public static OffsetDateTime lastDayOfWeek(OffsetDateTime time) {
        return time.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
    }

    public static OffsetDateTime firstDayOfMonth(OffsetDateTime time) {
        return time.with(TemporalAdjusters.firstDayOfMonth());
    }

    public static OffsetDateTime lastDayOfMonth(OffsetDateTime time) {
        return time.with(TemporalAdjusters.lastDayOfMonth());
    }

    public static OffsetDateTime firstDayOfYear(OffsetDateTime time) {
        return time.with(TemporalAdjusters.firstDayOfYear());
    }

    public static OffsetDateTime lastDayOfYear(OffsetDateTime time) {
        return time.with(TemporalAdjusters.lastDayOfYear());
    }

    public static OffsetDateTime now() {
        return OffsetDateTime.now();
    }

    public static int compare(OffsetDateTime time1, OffsetDateTime time2) {
        return time1.compareTo(time2);
    }

    public static String formatNow(String format) {
        OffsetDateTime dateTime = OffsetDateTime.now();
        return format(dateTime, format);
    }

    public static int getYear(OffsetDateTime time) {
        return time.getYear();
    }

    public static int getMonth(OffsetDateTime time) {
        return time.getMonth().getValue();
    }

    public static int getDayOfYear(OffsetDateTime time) {
        return time.getDayOfYear();
    }

    public static int getDayOfMonth(OffsetDateTime time) {
        return time.getDayOfMonth();
    }

    public static int getDayOfWeek(OffsetDateTime time) {
        return time.getDayOfWeek().getValue();
    }

    public static String format(OffsetDateTime time1, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        String formattedDateTime = time1.format(formatter);
        return formattedDateTime;
    }

}
